﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;

using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace ZLBHLuncher
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        static string processName = "ZLBH";
        private bool isRunning = false;
        private string destPath = "D:\\App\\ZLBH\\ZLBH.exe";
        private string destDllPath = "D:\\App\\ZLBH\\扶贫提醒.dll";
        private string currentPath = "";
        
        public MainWindow()
        {
            InitializeComponent();
            isRunning = check_app_running();
            currentPath = Environment.CurrentDirectory;
            text_app.Text = destPath;
            text_dll.Text = destDllPath;
        }

        private void button_origon_Click(object sender, RoutedEventArgs e)
        {
            warningInfo();
            FileInfo origonApp = new FileInfo(currentPath + "\\origon\\ZLBH.exe");
            CheckFileExists();
            origonApp.CopyTo(destPath);
            runZLBH();
        }

        private void button_hooked_Click(object sender, RoutedEventArgs e)
        {
            warningInfo();
            FileInfo origonApp = new FileInfo(currentPath + "\\hook\\ZLBH.exe");
            FileInfo origondll = new FileInfo(currentPath + "\\hook\\扶贫提醒.dll");
            CheckFileExists();
            origonApp.CopyTo(destPath);
            origondll.CopyTo(destDllPath);
            runZLBH();
        }
        private bool check_app_running() {
            if (System.Diagnostics.Process.GetProcessesByName("ZLBH").ToList().Count>0)
            {
                return true;
            }
            return false;
        }
        private void kill_zlbh_process()
        {
            System.Diagnostics.Process[] process = System.Diagnostics.Process.GetProcessesByName(processName);
            foreach (System.Diagnostics.Process p in process)
            {
                p.Kill();
            }
        }
        private void warningInfo()
        {
            if (isRunning)
            {
              MessageBoxResult result =  MessageBox.Show("zlbh.exe is still running, if continue,it will force kill the process","warning", MessageBoxButton.OKCancel);
                if (result==MessageBoxResult.OK)
                {
                    kill_zlbh_process();
                }
            }
        }
        private void runZLBH()
        {
            System.Diagnostics.Process p = System.Diagnostics.Process.Start(destPath);
        }

        private void CheckFileExists()
        {

            Thread.Sleep(1000);
            if (File.Exists(destPath))
            {
                File.Delete(destPath);
            }
            if (File.Exists(destDllPath))
            {
                File.Delete(destDllPath);
            }
        }

        private void browse_Click(object sender, RoutedEventArgs e)
        {
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            dialog.Description = "请选择中联BH路径";
            dialog.SelectedPath = "D:\\";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                destPath = dialog.SelectedPath + "\\ZLBH.exe";
                destDllPath = dialog.SelectedPath + "\\扶贫提醒.dll";
                text_app.Text = destPath;
                text_dll.Text = destDllPath;
            }
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }
    }
}
